package DAO.impl.hibernate;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import domain.Room;
import DAO.IRoomDAO;

@Transactional
@Component
public class RoomDaoImpl implements IRoomDAO {

	@SuppressWarnings("unused")
	private static Logger log = Logger.getLogger(RoomDaoImpl.class);
	private SessionFactory sessionFactory;

	public RoomDaoImpl() {}
	
	public RoomDaoImpl(SessionFactory sessionFactory) {
		super();
		this.sessionFactory = sessionFactory;
	}

	@Override
	public void CreateRoom(Room room) {
		Session session = sessionFactory.getCurrentSession();							
		session.saveOrUpdate(room);				
	}

	@Override
	public void UpdateRoom(Room room) {
		CreateRoom(room);
	}

	@Override
	public void DeleteRoom(Room room) {		
		Session session = sessionFactory.getCurrentSession();			
		session.delete(room);				
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Room> SelectRooms() {		
		Session session = sessionFactory.getCurrentSession();			
		List<Room> room = session.createQuery("from Room").list();						
		return room;		
	}

	@Override
	public Room SelectRoom(int id) {		
		Session session = sessionFactory.getCurrentSession();			
		Room room = (Room)session.get(Room.class, id);				
		return room;		
	}

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
}
